package com.tedu.udm.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.tedu.udm.pojo.dto.ClassAddNewDTO;
import com.tedu.udm.pojo.dto.ClassUpdateDTO;
import com.tedu.udm.pojo.vo.ClassListItemVO;
import com.tedu.udm.pojo.vo.ClassStandardVO;
import com.tedu.udm.service.IClassService;
import com.tedu.udm.web.JsonResult;
import io.jsonwebtoken.Jwts;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "99. 班级管理模块")
@Validated
@RestController
@RequestMapping("/classes")
public class ClassController {

    @Value("${udm.jwt.secret-key}")
    private String secretKey;

    @Autowired
    IClassService classService;

    public ClassController() {
        log.info("创建控制器对象：ClassController");
    }

    // http://localhost:9878/classes/add-new
    @ApiOperation("新增班级")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(ClassAddNewDTO classAddNewDTO){
        log.debug("开始处理【添加班级】的请求，参数：{}", classAddNewDTO);
        classService.addNew(classAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:9878/classes/9527/delete
    @ApiOperation("根据id删除班级")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "班级id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@Range(min = 1, message = "删除班级失败，尝试删除的班级的ID无效！")
                                   @PathVariable Long id) {
        log.debug("开始处理【根据id删除班级】的请求，参数：{}", id);
        classService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:9878/classes/9527/update
    @ApiOperation("修改班级信息")
    @ApiOperationSupport(order = 300)
    @PostMapping("/update")
    public JsonResult<Void> update(ClassUpdateDTO classUpdateDTO) {
        log.debug("开始处理【修改班级信息】的请求，参数：{}", classUpdateDTO);
        classService.update(classUpdateDTO);
        return JsonResult.ok();
    }

    // http://localhost:9878/classes/9527/update
    @ApiOperation("查询班级信息")
    @ApiOperationSupport(order = 400)
    @PostMapping("/{id:[0-9]+}/get")
    public JsonResult<ClassStandardVO> get(@Range(min = 1, message = "删除班级失败，尝试删除的班级的ID无效！")
                                    @PathVariable Long id) {
        log.debug("开始处理【查询班级信息】的请求，参数：{}", id);
        ClassStandardVO classStandardVO = classService.get(id);
        return JsonResult.ok(classStandardVO);
    }

    // http://localhost:9878/classes
    @ApiOperation("查询班级列表")
    @ApiOperationSupport(order = 401)
    @GetMapping("")
    public JsonResult<List<ClassListItemVO>> list() {
        log.debug("开始处理【查询班级列表】的请求，无参数");
        List<ClassListItemVO> list = classService.list();
        return JsonResult.ok(list);
    }

    // http://localhost:9878/classes
    @ApiOperation("查询所属班级列表")
    @ApiOperationSupport(order = 402)
    @RequiresPermissions(value = {"/users"})
    @GetMapping("my-class-list")
    public JsonResult<List<ClassListItemVO>> myClassList() {
        log.debug("开始处理【查询所属班级列表】的请求，无参数");
        String jwt = SecurityUtils.getSubject().getPrincipal().toString();
        Long id = Jwts.parser()
                      .setSigningKey(secretKey)
                      .parseClaimsJws(jwt)
                      .getBody()
                      .get("id", Long.class);
        List<ClassListItemVO> list = classService.myClassList(id);
        return JsonResult.ok(list);
    }

}
